package com.cn.jm.util;

import com.alibaba.fastjson.JSONObject;
import com.cn.jm.core.pay.wechat.MD5Util;
import com.cn.jm.core.utils.util.NoUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @description 黑名单
 * @date 2020/4/21
 */
public class BlackUtil {
    private static final String CHANNEL_ID = "";//商户号
    private static final String SERCET_KEY = "";//密钥
    private static final String URL = "http://39.105.178.155:9022/api/black/black98";//请求地址

    public static String checkBlack(String name, String idCard, String phone) throws IOException {
        //组装请求参数
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("idCard", idCard);
        map.put("phone", phone);
        String busiTransId = NoUtils.createOutTradeNo("B");;
        map.put("busiTransId", busiTransId);

        String jsonString = JSONObject.toJSONString(map);
//        System.out.println("jsonString = " + jsonString);

        String sign = MD5Util.MD5Encode(jsonString + SERCET_KEY, null);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(URL);
        post.setHeader("Content-Type", "application/json;charset=utf-8");
        post.setHeader("Accept", "application/json");
        post.setHeader("sign", sign);
        post.setHeader("channelId", CHANNEL_ID);
        post.setEntity(new StringEntity(jsonString, "utf-8"));
        HttpResponse httpResponse = httpClient.execute(post);
        HttpEntity response = httpResponse.getEntity();
        return EntityUtils.toString(response);
    }
}
